<template>
  <div class="messenger-box">
    <div class="list-box">
      <div class="list-header">
        <span>Message</span>
        <div class="add-btn">
          <span>+</span>
          <div>New</div>
        </div>
      </div>
      <div v-for="(item, index) in listMessage" :key="index" class="list">
        <div class="list-item" :class="item.imgs === viewObj.imgs ? 'active' : ''" @click="clickList(item)">
          <div class="avatar">
            <img :src="getImageUrl(item.imgs)">
          </div>
          <div class="information">
            <div class="name">
              {{ item.name }}
            </div>
            <div class="news">
              {{ getLastMessage(item.oldNewList) }}
            </div>
          </div>
        </div>
      </div>
    </div>
    <div class="message-box">
      <div class="heard-box">
        <div class="left-box">
          <div class="avatar">
            <img :src="getImageUrl(viewObj.imgs)">
            <span>{{ viewObj.name }}</span>
          </div>
          <span>|</span>
          <div class="status">
            Online
          </div>
        </div>
        <div class="right-box" />
      </div>
      <div class="body-box">
        <template v-if="viewObj.oldNewList?.length > 0">
          <div
            v-for="(item, index) in viewObj.oldNewList.filter(item => item.type === '1')" :key="index"
            class="new-item ts"
          >
            <img :src="getImageUrl(viewObj.imgs)">
            <div class="content-box">
              <span>{{ item.text }}</span>
              <div>{{ }}</div>
            </div>
          </div>
        </template>
        <template v-if="viewObj.oldNewList?.length > 0">
          <div
            v-for="(item, index) in viewObj.oldNewList.filter(item => item.type === '2')" :key="index"
            class="new-item"
          >
            <div class="content-box">
              <span>{{ item.text }}</span>
            </div>
            <img :src="getImageUrl(UseUser.userInfo.imgs)">
          </div>
        </template>
      </div>
      <div class="foot-box">
        <el-input v-model="newInformation" placeholder="Please input..." />
        <span @click="sendMessageFun">Send</span>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import moment from 'moment'
import useUserStore from '@/stores/modules/user.ts'

const UseUser = useUserStore()
const newInformation = ref('')
interface newList {
  type: string
  time: string
  text: string
}
interface MessageInfo {
  imgs: string
  name: string
  oldNewList: newList[]
}
const listMessage = reactive<MessageInfo[]>([
  {
    imgs: 's-1',
    name: 'Bai Peng yang',
    oldNewList: [
      {
        type: '1',
        time: '2025-08-28 11:11:40',
        text: '你好呀，请问你收到消息了吗？',
      },
      {
        type: '1',
        time: '2025-08-28 11:11:45',
        text: '方便给我发一份你的简历吗？',
      },
      {
        type: '2',
        time: '2025-08-29 09:21:30',
        text: '我不喜欢你们公司，不好意思！',
      },
    ],
  },
  {
    imgs: 's-2',
    name: 'Cao Rong sen',

    oldNewList: [
      {
        type: '1',
        time: '2025-08-29 09:11:45',
        text: '你好呀，请问还需要找工作吗？',
      },
      {
        type: '1',
        time: '2025-08-29 10:11:45',
        text: '方便给我发一份你的简历吗？',
      },
      {
        type: '2',
        time: '2025-08-30 09:11:45',
        text: '可以的！',
      },
    ],
  },
  {
    imgs: 's-3',
    name: 'Liu Si chen',
    oldNewList: [
      {
        type: '1',
        time: '2025-08-31 00:00:02',
        text: '你好啊兄弟，打三角洲吗！',
      },
      {
        type: '2',
        time: '2025-08-31 10:00:00',
        text: '起这么早！',
      },
    ],

  },
])
const getLastMessage = (messageList: any[]) => {
  if (!messageList || messageList.length === 0) { return '' }
  const latestItem = messageList.reduce((prev, current) => {
    return new Date(prev.time) > new Date(current.time) ? prev : current
  })
  return latestItem.text
}
const viewObj = ref({} as MessageInfo)
function clickList(row) {
  viewObj.value = row
}
function sendMessageFun() {
  listMessage.forEach((item) => {
    if (item.imgs === viewObj.value.imgs) {
      nextTick(() => {
        item.oldNewList.push({
          type: '2',
          text: newInformation.value,
          time: moment().format('YYYY-MM-DD HH:mm:ss'),
        })
        newInformation.value = ''
      })
    }
  })
}
function getImageUrl(img: string): string {
  return new URL(`/src/assets/pic/${img}.png`, import.meta.url).href
}
</script>

<style lang="scss" scoped>
.messenger-box {
  margin: 60px auto;
  width: 80%;
  height: 80%;

  display: flex;
  background: #FFFFFF;

  .list-box {
    width: 375px;
    height: 100%;
    border-right: 1px solid gainsboro;
    display: flex;
    flex-direction: column;
    padding: 20px;
    box-sizing: border-box;

    .list-header {
      display: flex;
      align-items: center;
      justify-content: space-between;
      margin-bottom: 20px;

      &>span {
        font-size: 25px;
        color: #171725;
      }

      .add-btn {
        padding: 10px 15px;
        box-sizing: border-box;
        background: #0062FF;
        border-radius: 10px;
        display: flex;
        align-items: center;

        &>span {
          font-size: 19px;
          color: white;
          margin-right: 15px;
        }

        &>div {
          font-size: 15px;
          color: white;
        }
      }
    }

    .list {
      display: flex;
      flex-direction: column;

      .list-item {
        cursor: pointer;
        width: 100%;
        height: 80px;
        border-radius: 5px;
        display: flex;
        align-items: center;
        padding: 0 16px;
        box-sizing: border-box;
        background: rgb(240, 246, 248);
        margin-bottom: 16px;

        .avatar {
          width: 40px;
          height: 40px;
          margin-right: 16px;

          &>img {
            width: 100%;
            height: 100%;
            border-radius: 50%;
          }
        }

        .information {
          display: flex;
          flex-direction: column;

          .name {
            font-size: 18px;
            color: #171725;
            font-weight: 600;
          }

          .news {
            color: #59595a;
            font-size: 14px;
          }
        }
      }

      .list-item:hover {
        background: rgb(141, 178, 247);
      }

      .list:last-child {
        margin-bottom: 0;
      }
    }
  }

  .message-box {
    flex: 1;
    display: flex;
    flex-direction: column;
    justify-content: space-between;

    .heard-box {
      height: 84px;
      display: flex;
      justify-content: space-between;
      padding: 0 16px;
      align-items: center;
      border-bottom: 1px solid #171725;

      .left-box {
        display: flex;
        align-items: center;

        .avatar {

          display: flex;
          align-items: center;
          width: auto;

          &>img {
            margin-right: 10px;
            width: 30px;
            height: 30px;
            border-radius: 50%;
          }

          &>span {

            font-size: 16px;
            font-weight: 600;
          }
        }

        &>span {
          margin: 0 15px;
        }
      }

      .right-box {
        width: 20px;
        height: 20px;
        background: url("@/assets/pic/phoneIcon.png") no-repeat;
        background-size: 100% 100%;
      }
    }

    .body-box {
      height: calc(100% - 200px);
      overflow-y: auto;
      overflow-x: hidden;
      display: flex;
      flex-direction: column;

      .new-item {
        display: inline-flex;
        padding: 10px 15px;
        display: flex;
        justify-content: flex-end;
        align-items: center;

        .content-box {
          background: #E2E2EA;
          border-radius: 15px;
          padding: 19px 20px;
          box-sizing: border-box;
        }

        &>img {
          margin-left: 10px;
          width: 34px;
          height: 34px;
          border-radius: 50%;
        }
      }

      .ts {
        justify-content: flex-start !important;

        &>img {
          margin-left: 0 !important;
          margin-right: 10px !important;
        }
      }
    }

    .foot-box {
      height: 88px;
      display: flex;
      background: #eaeaf0;
      align-items: center;
      padding: 0 20px;

      &>span {
        margin-left: 20px;
        cursor: pointer;
        padding: 5px 10px;
        box-sizing: border-box;
        border: 1px solid;
        border-radius: 5px;

        &:hover {
          background: #0062FF;
          color: white;
        }
      }
    }
  }
}

.active {
  background: rgb(141, 178, 247) !important;
}
</style>
